/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-04-04
 * Time:16:31
 */
public class FindKthToTail {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * BM8链表中倒数最后k个结点
     *
     * @param pHead ListNode类
     * @param k int整型
     * @return ListNode类
     */
    public ListNode FindKthToTail (ListNode pHead, int k) {
        // write code here
        ListNode output = pHead;
        ListNode cur = pHead;
        if (pHead == null){
            return null;
        }
        int count = 0;
        while (count < k && cur != null){
            //让cur先走k步
            //让 count 计数，在后面判断 k 是否大与 count，到最后 count 实际得到的是pHead的总节点数
            count++;
            cur = cur.next;
        }
        while (cur != null){
            cur = cur.next;
            output = output.next;
            count++;
        }
        if (count < k){
            return null;
        }
        return output;
    }
}
